feat(database): add shared utilities module#4
Merged
maltonoloco merged 13 commits intoOpenTaberna:mainfrom Dec 7, 2025
Merged
Conversation
Centralize common imports and fallback exceptions to eliminate code duplication across database components. Reduces ~100 lines of duplicated try/except blocks. - Add get_logger() with fallback NullLogger - Add DatabaseError, NotFoundError, InternalError fallback classes - Add get_settings() import helper - Enable optional dependencies for shared modules
Implement declarative base and reusable mixins for database models. - Add Base class with to_dict() and __repr__() methods - Add TimestampMixin for automatic created_at/updated_at tracking - Add SoftDeleteMixin with soft_delete() and restore() methods - Use UTC timezone for all timestamp fields - Enable generic model functionality across all tables
Implement async PostgreSQL engine with connection pooling and lifecycle management. - Add create_engine() with configurable connection pool - Add create_test_engine() with NullPool for testing - Add init_database()/close_database() for app lifecycle - Add get_engine() singleton accessor - Configure connection pool with overflow and timeout settings - Integrate error handling with DatabaseError/InternalError - Add comprehensive logging for all operations
Implement async session factory with context managers and FastAPI dependency injection support. - Add create_session_factory() with configurable options - Add get_session() context manager with auto-commit/rollback - Add get_session_dependency() for FastAPI Depends() - Wrap all errors in DatabaseError for consistent handling - Enable expire_on_commit=False for post-commit access - Integrate with engine module for database connection
Implement type-safe generic CRUD repository with comprehensive query operations. - Add BaseRepository[ModelType] with Generic[T] for type safety - Add CRUD methods: get, get_by, get_all, filter with pagination - Add bulk operations: create_many, update_many, delete_many - Add helper methods: count, exists, get_or_raise, get_by_or_raise - Add execute() for custom SQL queries - Integrate NotFoundError for missing entities - Add soft delete support via is_deleted filter - Enable reusable repository pattern across all models
Implement transaction context manager with savepoint support for nested transactions. - Add transaction() context manager with auto-commit/rollback - Add nested transaction support via savepoints - Wrap all errors in DatabaseError - Enable manual transaction control in async contexts - Integrate with session module for database operations
Implement database health checks and connection monitoring. - Add check_database_health() with connectivity test and latency - Add get_database_info() with version, connections, pool stats - Return structured HealthStatus and DatabaseInfo dataclasses - Measure query execution time for performance monitoring - Query PostgreSQL system tables for connection statistics - Enable health endpoint integration for production monitoring
Implement Alembic migration utilities for schema management. - Add get_alembic_config() for configuration loading - Add run_migrations() for applying pending migrations - Add rollback_migration() for reverting changes - Add create_migration() for generating new migrations - Add get_migration_history() for tracking applied migrations - Configure async database URL for Alembic - Enable programmatic migration control - Integrate logging for all migration operations
Export database module public interface for clean imports. - Export engine functions: init_database, close_database, get_engine - Export session utilities: get_session, get_session_dependency, AsyncSession - Export base classes: Base, TimestampMixin, SoftDeleteMixin - Export repository: BaseRepository - Export transaction: transaction - Export health: check_database_health, get_database_info - Export migrations: run_migrations, rollback_migration, create_migration - Enable clean import syntax: from app.shared.database import ...
Add comprehensive tests for database module structure and imports. - Add 23 tests covering all database components - Test utils fallback classes and logger integration - Test Base model methods (to_dict, __repr__) - Test TimestampMixin and SoftDeleteMixin attributes - Test all module imports and public API exports - Test repository CRUD method existence - Test migration utility functions - Verify module structure and interface consistency
Add complete documentation for database module usage and architecture. - Add Quick Start guide with setup and basic usage - Document configuration options and environment variables - Explain Base model and mixin usage patterns - Document repository pattern with CRUD examples - Cover session management and dependency injection - Explain transaction handling and nested transactions - Document health checks and monitoring - Cover Alembic migrations and schema management - Add error handling best practices - Include testing guidelines and examples - Add troubleshooting section for common issues
maltonoloco
approved these changes
Dec 7, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Centralize common imports and fallback exceptions to eliminate
code duplication across database components. Reduces ~100 lines
of duplicated try/except blocks.
Description
Setup database modul. Database usage is now possible.
Scope
Motivation
For better dev and a step to prod release.
Testing Strategy
Documentation
Checklist